var dataUrl = null;
$(function () {
    init();
    $("#startTime").val(getCurrentTime());
    $("form").validate({
        submitHandler: function (form) {// 必须写在验证前面，否则无法ajax提交
            //jquery获取复选框值
            // var chk_value =[];//定义一个数组
            var excutePersonId = "";
            $('input[name="checkPerson"]:checked').each(function () {//遍历每一个名字为interest的复选框，其中选中的执行函数
                excutePersonId += $(this).val() + ",";//拼接检查人
                // chk_value.push($(this).val());//将选中的值添加到数组chk_value中

            });
            $("#excutePersonId").val(excutePersonId);

            var projectName = $("#projectId").find("option:selected").text();
            $("#projectName").val(projectName);
            if (!checkForm()) {
                return;
            }
            $("#propId").val($("#propName").find("option:selected").attr("data-id"));
            var index = layer.load(1, {shade: false});
            $("#submitButton").attr("disabled", "disabled");
            $(form).ajaxSubmit({
                dataType: "text",
                success: function (data) {
                    $("#submitButton").removeAttr("disabled");
                    layer.close(index);
                    if (data == "success") {
                        // layer.confirm('添加成功!是否关闭窗口?', function (index) {
                        // parent.grid.loadData();
                        parent.location.reload();
                        parent.layer.close(parent.pageii);
                        return false;
                        // });
                    } else {
                        layer.alert('添加失败！', 3);
                    }
                }
            });
        },
        errorPlacement: function (error, element) {// 自定义提示错误位置
            $(".l_err").css('display', 'block');
            // element.css('border','3px solid #FFCCCC');
            $(".l_err").html(error.html());
        },
        success: function (label) {// 验证通过后
            $(".l_err").css('display', 'none');
        }
    });
});

function init() {
    // dataUrl = $("#dataUrl").val();
    dataUrl = "/modules";
    getPropList();
    getProjectStageList();
    getResponsibilityCropList();
    getInspectPersonList();
    getProjectNameList();

}

function checkForm() {
    if ($("#projectId").val() == '') {
        layer.msg('请选择工程名称');
        return false;
    }
    if ($("#projectStage").val() == '') {
        layer.msg('请选择工程阶段');
        return false;
    }
    if ($("#propName").val() == '') {
        layer.msg('请选择库名称');
        return false;
    }

    if ($("#responsibilityCrop").val() == '') {
        layer.msg('请选择责任单位');
        return false;
    }
    if ($("#responsibilityPerson").val() == '') {
        layer.msg('请选择责任人');
        return false;
    }
    if ($("#excutePersonId").val() == '') {
        layer.msg('请选择检查人');
        return false;
    }
    if ($("#startTime").val() == '') {
        layer.msg('触发时间不能为空');
        return false;
    }
    if ($("#endTime").val() == '') {
        layer.msg('截止时间不能为空');
        return false;
    }
    return true;
}

/**
 * 获取当前时间 格式：yyyy-MM-dd HH:MM:SS
 */
function getCurrentTime() {
    var date = new Date();//当前时间
    var month = zeroFill(date.getMonth() + 1);//月
    var day = zeroFill(date.getDate());//日
    var hour = zeroFill(date.getHours());//时
    var minute = zeroFill(date.getMinutes());//分
    var second = zeroFill(date.getSeconds());//秒

    //当前时间
    var curTime = date.getFullYear() + "-" + month + "-" + day
        + " " + hour + ":" + minute + ":" + second;

    return curTime;
}

/**
 * 补零
 */
function zeroFill(i) {
    if (i >= 0 && i <= 9) {
        return "0" + i;
    } else {
        return i;
    }
}

function checkedInspectPerson() {
    pageii = layer.open({
        title: ['检查人'],
        // title : "发布任务",
        type: 1,
        fixed: true,
        shadeClose: false,
        area: ["420px", "60%"],
        btn: ['确定'],
        content: $("#inspectPerson"),
        yes: function (index, layero) {
            var inspectPersonSpan = "";
            $('input[name="checkPerson"]:checked').each(function () {//遍历每一个名字为interest的复选框，其中选中的执行函数
                var id = $(this).val();
                inspectPersonSpan += $("#span" + id).text() + ",";//拼接检查人
                // chk_value.push($(this).val());//将选中的值添加到数组chk_value中

            });
            $("#inspectPersonSpan").text(inspectPersonSpan);
            layer.close(index);
        }
    });
}

/**
 * 项目工程下拉列表
 */
function getProjectNameList() {
    var url = dataUrl + "/homepage/allProject?personId=" + $("#personId").val();
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].caption) + "</option>";
                }
                $("#projectId").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}

/**
 * 责任单位下拉列表
 */
function getResponsibilityCropList() {
    var url = dataUrl + "/corp/all";
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].caption) + "</option>";
                }
                $("#responsibilityCrop").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}

/**
 * 责任人下拉列表
 * @param cropId 单位id
 */
function getResponsibilityPersonList(corpId) {
    var url = dataUrl + "/projectManage/persons?corpId=" + corpId;
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].id + "'>" + (result[i].name) + "</option>";
                }
                $("#responsibilityPerson").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}

/**
 * 检查人下拉列表
 */
var inspectNameDateList = null;

function getInspectPersonList() {
    var url = dataUrl + "/projectManage/all_person";
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            // html += "<input type='text' />"
            if (result != null) {
                inspectNameDateList = result;
                for (var i = 0; i < result.length; i++) {
                    html += "<input name='checkPerson' type='checkbox' value='" + result[i].id + "'/><span id='span" + result[i].id + "' style='display: inline-block;width: 80px'>" + (result[i].name) + "</span>";
                    if (i>0&&(i +1) % 4 == 0) {
                        html += "<br/>";
                    }
                }
                $("#inspectPersonList").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}

/**
 * 库名称下拉列表
 */


function getPropList() {
    var url = dataUrl + "/basic/prop/all";
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            html += "<option  value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option data-id='" + result[i].id + "' value='" + result[i].caption + "'>" + (result[i].caption) + "</option>";
                }
                $("#propName").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}

/**
 * 项目阶段下拉列表
 */
function getProjectStageList() {
    var url = dataUrl + "/projectStage/all";
    $.ajax({
        url: url,
        method: "get",
        dataType: "json",
        success: function (result) {
            var html = "";
            html += "<option value=''>------请选择------</option>";
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    html += "<option value='" + result[i].stageID + "'>" + (result[i].stageCaption) + "</option>";
                }
                $("#projectStage").html(html);
            } else {
                layer.msg("获取列表信息错误，请联系管理员！");
            }
        }
    });
}


//str字符串是否包含substr字符串
function isContains(str, substr) {
    return str.indexOf(substr) >= 0;
}

//layui模板的写法
// var getTpl = searchText.innerHTML;//写到js方法外边这样只加载一次，不用每次都加载，速度快
/* 前端页面的搜索 */
function ck(campusName) {
    var html = ""
    if (inspectNameDateList) {
        getInspectNameIds();
        var j = 0;
        for (var i = 0; i < inspectNameDateList.length; i++) {
            html += "<input name='checkPerson' type='checkbox' ";
            html += checkInspectNameIds(inspectNameDateList[i].id) ? "checked='checked'" : ''
            if (!isContains(inspectNameDateList[i].name, campusName) && !checkPinYin(inspectNameDateList[i].name, campusName)) {
                html += " hidden "
            }
            html += "value='" + inspectNameDateList[i].id + "'/>" +
                "<span id='span" + inspectNameDateList[i].id + "'";
            if (!isContains(inspectNameDateList[i].name, campusName) && !checkPinYin(inspectNameDateList[i].name, campusName)) {
                html += " style='display:none'"
            } else {
                html += " style='display: inline-block;width: 80px'"

            }

            html += ">" + (inspectNameDateList[i].name) + "</span>";
            if (isContains(inspectNameDateList[i].name, campusName) || checkPinYin(inspectNameDateList[i].name, campusName)) {

                if (j>0&&(j + 1) % 4 == 0) {
                    html += "<br/>";
                }
                j++;
            }
        }
    }
    $("#inspectPersonList").html(html);
}

function checkPinYin(name, campusName) {
    var pinyinName = ConvertPinyin(name)
    return isContains(pinyinName, campusName)
}

var inspectNameIds = null;

function getInspectNameIds() {
    inspectNameIds = [];
    $('input[name="checkPerson"]:checked').each(function () {//遍历每一个名字为interest的复选框，其中选中的执行函数
        inspectNameIds.push($(this).val());//将选中的值添加到数组chk_value中
    });
}

function checkInspectNameIds(id) {
    if (inspectNameIds != null && inspectNameIds.length > 0) {
        for (var i = 0; i < inspectNameIds.length; i++) {
            if (inspectNameIds[i] == id.toString()) {
                return true
            }
        }
    }
    return false
}